MSBUILD=/cygdrive/c/Windows/Microsoft.NET/Framework/v3.5/msbuild.exe
TYBUILD=../tybuild/bin/Release/tybuild.exe
TYSILA=../tysila2/bin/Release/tysila2.exe
PEKERNEL=bin/Release/tysos.exe
MSCORLIB=../mono/corlib/mscorlib.obj
LD=x86_64-elf-ld
BZIP2=bzip2

UNIMPL_LABELS=../mono/corlib/mscorlib-unimplemented.txt
MSCORLIB_SYMS=mscorlib_syms.txt

KERNELOBJ=tysos.obj
KERNEL=tysos.bin
KERNELBZ2=tysos.bin.bz2

LDFLAGS=-pie
LDADD=
BZIP2FLAGS=-kf
TYSILAFLAGS=-L../mono/corlib -q -d --whole-module --irdump tysos.ir.txt
#TYSILAFLAGS=-L../mono/corlib -q -d --extra-type _m8_mscorlib_N6_System_T6_Object_S2_1C --extra-method _m8_mscorlib_N6_System_T6_String_S2_0E_M10_get_Length_S6_200008 --extra-method _m8_mscorlib_N6_System_T6_Object_S2_1C_M5_.ctor_S6_200001 --irdump tysos.ir.txt

.PHONY: clean $(PEKERNEL)

all: $(KERNELBZ2)

$(TYSILA):
	cd ../tysila2 && make

$(MSCORLIB):
	cd ../mono/corlib && make

$(MSCORLIB_SYMS): $(MSCORLIB)
	./make_mscorlib_syms.sh

$(KERNELBZ2): $(KERNEL)
	$(BZIP2) $(BZIP2FLAGS) $(KERNEL)

$(KERNEL): $(KERNELOBJ) halt.o cpu.o undefined.o arith.o x86_64/switcher.o $(MSCORLIB)
	$(LD) $(LDFLAGS) -o $(KERNEL) $(KERNELOBJ) halt.o cpu.o arith.o undefined.o x86_64/switcher.o $(MSCORLIB) $(LDADD)
	x86_64-elf-objdump -d $(KERNEL) > ../tysos.txt
	x86_64-elf-objdump -d $(KERNELOBJ) > ../tysos.obj.txt

halt.o: halt.asm
	yasm -f elf64 -o halt.o halt.asm

cpu.o: cpu.asm
	yasm -f elf64 -o cpu.o cpu.asm

arith.o: arith.asm
	yasm -f elf64 -o arith.o arith.asm

x86_64/switcher.o: x86_64/switcher.asm
	yasm -f elf64 -o x86_64/switcher.o x86_64/switcher.asm

undefined.asm: $(UNIMPL_LABELS)
	cp $(UNIMPL_LABELS) ./unimplemented.txt
	./make_undefined.sh

undefined.o: undefined.asm
	yasm -f elf64 -o undefined.o undefined.asm

$(KERNELOBJ): $(TYSILA) $(PEKERNEL) $(MSCORLIB_SYMS)
	$(TYSILA) $(TYSILAFLAGS) -o $(KERNELOBJ) --exclude $(MSCORLIB_SYMS) $(PEKERNEL)

$(PEKERNEL): $(TYBUILD)
	$(TYBUILD) /p:Configuration=Release /v /unsafe /tools:2

clean:
	rm -rf obj bin $(PEKERNEL) $(KERNELOBJ) $(KERNEL) $(KERNELBZ2)

